import { observable, action } from 'mobx';
import * as Apis from '../../apis/Api';

export default class ApiGroupShowStore {
    @observable requestParam = 'false';
    @observable groups = [];// 菜单分组
    @observable module = '';
    @observable info = [];
    @observable apiDataList = [];
    @observable groupId = '';
    @observable msgCode = '';
    @observable isSuccess = false;
    @observable disabled = false;
    @observable loading = true;
    @observable params = {};
    // 多接口页面获取菜单
    @action
    getApiMenu = (props) => {
      if (props.location) { // 通过链接
        this.requestParam = props.location.query;
        this.params = props.location.query;
        this.module = props.params.module;
      } else { // 通过组件
        this.module = props.module;
        this.requestParam = props.requestPara;
        this.params = props.requestPara;
      }
      if (this.requestParam.EtpFullName) {
        this.name = this.requestParam.EtpFullName;
        // console.log(props);
        Apis.getApi({ apiId: '3e0c6c21300f4825a21679754e84f890', query: JSON.stringify(this.requestParam), isList: '0' }).then((data) => {
          // console.log("第一次请求工商信息！");
          if (data.status != 'success') { // 根据现有参数获取的工商你那个信息为空，处理企业名称并重新请求
            this.GetEnterpriseSubName();
          } else {
            this.getMenu();
          }
        });
      } else {
        this.getMenu();
      }
    }

    @action
    GetEnterpriseSubName = () => {
      Apis.getEnterpriseSubName({ name: this.name }).then((data) => {
        this.requestParam.EtpFullName = data.data;
        this.requestParam.keyword = data.data;
        Apis.getApi({ apiId: '3e0c6c21300f4825a21679754e84f890', query: JSON.stringify(this.requestParam), isList: '0' }).then((data) => {
          // console.log("第二次请求工商信息，处理完企业名称后如果还未获取到工商信息，则跳转到匹配列表中，页面中菜单点击失效。");
          // console.log(data);
          if (data.status == 'success') {
            this.getMenu();
          } else {
            this.disabled = true;
            this.loading = false;
          }
        });
      });
    }
    /**
     * 获取分组信息
     */
    @action
    getMenu = () => {
      Apis.getMenu({ module: this.module }).then((data) => {
        this.loading = true;
        if (data.status == 'success') {
          this.groups = data.data;
          !this.disabled && (this.groupId = data.default_group);
          this.InitGroupInfo(data.default_group);
          this.isSuccess = true;
        } else {
          this.msgCode = data.msgcode;
        }
        this.loading = false;
      });
    }
    /** 下面处理多接口页面按分组加载 * */
    // 点击子菜单加载下面的数据
    @action
    InitGroupInfo = (groupId, firstId) => {
      this.info = [];
      this.groupId = groupId;
      Apis.getConditionByGroup({ module: this.module, groupId, firstId }).then((data) => {
        if (data.status == 'success') {
          this.info = data.data;
          this.apiDataList = data.apiDataList;
          this.isSuccess = true;
        } else {
          this.msgCode = data.msgcode;
        }
      });
    }

    // 在A菜单中点击B菜单的子菜单项,先加载B组全组的数据，再将点击的子菜单放到第一项
    @action
    onClickMenuItem = (id) => {
      Apis.getGroupIdByApifront({ id }).then((data) => {
        if (data.status == 'success') {
          this.groupId = data.groupId;
          this.InitGroupInfo(data.groupId, id);
          this.menuToTop(id);
          this.isSuccess = true;
        } else {
          this.msgCode = data.msgcode;
        }
      });
    }

    // 刷新分组后的数量
    @action
    reFresh = (id, total) => {
      this.groups.map((group) => {
        group.item.map((item) => {
          if (id == item.itemId) {
            item.number = total;
          }
        });
      });
    }
    // 将跨分组点击后的菜单挪到第一位
    @action
    menuToTop = (id) => {
      this.groups.map((group) => {
        group.item.map((item, index) => {
          if (id == item.itemId) {
            const temp = group.item[0];
            group.item[0] = group.item[index];
            group.item[index] = temp;
          }
        });
      });
    }
}
